package com.sduept.nwld.dataserver.controller.statistics;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.statistics.ElectricityGenerationManager;
import com.sduept.nwld.dataserver.model.countanalyse.ElectricityInfo;
import org.primefaces.event.SelectEvent;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class ElectricityGenerationController extends AbstractController{

	private static final long serialVersionUID = 2308805100553212355L;
	
	@Autowired
	private ElectricityGenerationManager manager;
	
	private List<ElectricityInfo> electricityInfos = new ArrayList<ElectricityInfo>();
	private ElectricityInfo electricityInfo = new ElectricityInfo();
	private ElectricityInfo select = new ElectricityInfo();
	private String voltage;
	private String year;
	
	@PostConstruct
	private void init() {
		electricityInfos = manager.getAllElectricityInfo();
	}
	
	public void preCreat() {
		electricityInfo = new ElectricityInfo();
		year = null;
		voltage = null;
	}
	
	public void preEdit() {
		electricityInfo = select;
		year = select.getYear().toString();
		voltage = select.getVoltage().toString();
	}
	
	public void saveElectricityInfo() {
		if(electricityInfo != null) {
			if(null != year) {
				electricityInfo.setYear(Integer.parseInt(year));
			}
			if(null != voltage && !"0".equals(voltage)) {
				electricityInfo.setVoltage(Integer.parseInt(voltage));
			}
			if(electricityInfo.getId() == null) {
				// 新建
				if(!isExist(electricityInfo)) {
					ElectricityInfo ei = manager.createOrUpdateElectricityInfo(electricityInfo);
					electricityInfos.add(ei);
				} else {
					addErrorMessage("该条件下数据已存在！");
					return;
				}
			} else {
				ElectricityInfo ei = manager.createOrUpdateElectricityInfo(electricityInfo);
				electricityInfos.set(electricityInfos.indexOf(electricityInfo), ei);
				select = new ElectricityInfo();
			}
			addSuccessMessage("保存成功");
		}
	}

	private boolean isExist(ElectricityInfo current) {
		List<ElectricityInfo> lt = manager.getElectricityInfos(current.getYear(), current.getVoltage());
		if(lt.size()>0) {
			return true;
		} else {
			return false;
		}
	}
	
	public void deleteElectricityInfo() {
		if(select != null && !(select.getId().isEmpty())) {
			manager.deleteElectricityInfo(select.getId());
			electricityInfos.remove(select);
			select = new ElectricityInfo();
			addSuccessMessage("删除成功！");
		} else {
			addErrorMessage("请首先选择记录！");
		}
	}

	public void onRowSelect(SelectEvent event) {
		select = (ElectricityInfo) event.getObject();
	}
	
	public List<ElectricityInfo> getElectricityInfos() {
		return electricityInfos;
	}

	public void setElectricityInfos(List<ElectricityInfo> electricityInfos) {
		this.electricityInfos = electricityInfos;
	}

	public ElectricityInfo getElectricityInfo() {
		return electricityInfo;
	}

	public void setElectricityInfo(ElectricityInfo electricityInfo) {
		this.electricityInfo = electricityInfo;
	}

	public ElectricityInfo getSelect() {
		return select;
	}

	public void setSelect(ElectricityInfo select) {
		this.select = select;
	}

	public String getVoltage() {
		return voltage;
	}

	public void setVoltage(String voltage) {
		this.voltage = voltage;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

}
